//
//  OverviewController.m
//  Test
//
//  Created by Hieu Nguyen Trung on 2/7/13.
 //

#import "OverviewController.h"
#import "OverviewItemCell.h"
#import "ActSettingViewController.h"
#import "ActVoucherViewController.h"
#import "ActGlobal.h"
#import "ActPaymentSelectionViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "JobViewController.h"
#import "ActPayrollCalculatorFeeViewController.h"

#define ARRAY_LABEL @[@"Toyota Commercial", @"Toyota Commercial", @"Toyota Commercial", @"Nixon Watches Spot", @"Modern Family", @"Homeland", @"Homeland", @"Modern Family"]
static int arrayPrice[] = {900, 900, 768, 300, 1800, 325, 325, 1800};
static int arrayDays[] = {2, 3, 8, 30, 38, 50, 50, 38};
static float arrayProgress[] = {0.5, 0.9, 0.25, -1, -1, -1, -1, -1, -1};

@interface OverviewController ()

@end

@implementation OverviewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initializeData];
    [self initializeView];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_searchBar release];
    [_tableView release];
    [_navigationView release];
    [super dealloc];
}

- (void)viewDidUnload {
    [self setNavigationView:nil];
    [super viewDidUnload];
}

-(void) viewDidAppear:(BOOL)animated{
    // do nothing
    [self.navigationView setNeedsDisplay];
}

-(void) showSubviews:(UIView *)view{
    if ([[view subviews] count] == 0){
        NSLog(@"%@", view);
        return;
    }
    for (UIView *subView in [view subviews]){
        [self showSubviews:subView];
    }
}

#pragma mark - Initialization Section
-(void) initializeData{
    
}

-(void) initializeView{
    [self.navigationView.btnBack setBackgroundImage:[UIImage imageNamed:@"backbar_button_root"] forState:UIControlStateNormal];
    [self.navigationView.btnBack setUserInteractionEnabled:false];
    [self.navigationView.btnRight2 setBackgroundImage:[UIImage imageNamed:@"buttonAdd"] forState:UIControlStateNormal];
    [self.navigationView.btnRight2 addTarget:self action:@selector(buttonAddClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.btnRight1 addTarget:self action:@selector(doSetting:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    
    [self.searchBar setSearchTextPositionAdjustment:UIOffsetMake(33, 0)];
    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"SearchBarBackground.png"]];
    [self.searchBar setImage:[UIImage imageNamed:@"SearchIcon.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"SearchFieldBackground.png"] forState:UIControlStateNormal];
    [self.searchBar setDelegate:self];
    
    for (UIView *view in [self.searchBar subviews]){
        if ([view isKindOfClass:[UITextField class]]){
            [((UITextField *)view) setLeftViewMode:UITextFieldViewModeNever];
            [((UITextField *)view) setClearButtonMode:UITextFieldViewModeWhileEditing];
        }
    }
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 8;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 58;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OverviewItemCell *cell = nil;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"OverviewItemCell" owner:self options:nil];
    for (id currentObject in objectsInXIB){
        if ([currentObject isKindOfClass:[OverviewItemCell class]]){
            cell = currentObject;
            int index = indexPath.row;
            [cell.layer setBorderColor:[UIColor lightGrayColor].CGColor];
            [cell.layer setBorderWidth:0.5];
            [cell.labelTitle setText:ARRAY_LABEL[index]];
            [cell.labelPrice setText:[NSString stringWithFormat:@"$ %d", arrayPrice[index]]];
            [cell.labelDays setText:[NSString stringWithFormat:@"%d days ago", arrayDays[index]]];
            [cell.progressView setProgress:arrayProgress[index]];
            if (arrayProgress[index] >= 0){
                [cell setHasProgressView:true];
            }
            else{
                [cell setHasProgressView:false];
            }
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
    }
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[tableView cellForRowAtIndexPath:indexPath] setSelected:false];
    if (indexPath.row <= 2){
        JobViewController *jobVC = [[[JobViewController alloc] initWithScreenType:JobEdit] autorelease];
        [self.tabBarController.navigationController pushViewController:jobVC animated:true];
    }
    else if (indexPath.row == 3){
        JobViewController *jobVC = [[[JobViewController alloc] initWithScreenType:JobReview] autorelease];
        [self.tabBarController.navigationController pushViewController:jobVC animated:true];
    }
}

#pragma mark - Actions Handle
-(void) buttonAddClick{
    JobViewController *jobVC = [[[JobViewController alloc] initWithScreenType:JobCreate] autorelease];
    [self.tabBarController.navigationController pushViewController:jobVC animated:true];
}

- (void)doVoucher:(id)sender{
    ActPayrollCalculatorFeeViewController *controller = [[ActPayrollCalculatorFeeViewController alloc] initWithType:ActPayrollCaculatorTypeFree];
    
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}
- (IBAction)doSetting:(id)sender{
//    ActPayrollCalculatorFeeViewController *controller = [[ActPayrollCalculatorFeeViewController alloc] initWithType:ActPayrollCaculatorTypeCustomsAllowance];
//    //ActVoucherViewController *controller = [[ActVoucherViewController alloc] initWithNibName:@"ActVoucherViewController" bundle:nil];
//    //    ActPaymentSelectionViewController *controller = [[ActPaymentSelectionViewController alloc] initWithPaymentSelectionType:ActPaymentSelectionTypeEntertainment];
//    //    ActPaymentSelectionViewController *controller = [[ActPaymentSelectionViewController alloc] initWithPaymentSelectionType:ActPaymentSelectionTypeEntertainment];
//    
//    controller.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:controller animated:YES];
//    //    [self.delegate  overViewController:self didSelectAdd:controller];
//    [controller release];
    ActSettingViewController *controller = [[ActSettingViewController alloc] initWithNibName:@"ActSettingViewController" bundle:nil];
//    controller.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:controller animated:YES];

//    [((UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController) pushViewController:controller animated:true];
    [self.tabBarController.navigationController pushViewController:controller animated:true];
    [controller release];
}

#pragma mark - UISearchBarDelegate Methods Implementation
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}

@end
